package com.conv.shop.business.service;

import com.conv.shop.business.domain.Consumer;
import com.conv.shop.business.domain.Store;
import com.conv.shop.business.domain.StoreUser;
import com.conv.shop.business.domain.StoreUserExample;
import com.conv.shop.business.mapper.ConsumerMapper;
import com.conv.shop.business.mapper.ProductMapper;
import com.conv.shop.business.mapper.StoreMapper;
import com.conv.shop.business.mapper.StoreUserMapper;
import com.conv.shop.business.req.StoreReq;
import com.conv.shop.business.resp.StoreUserResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StoreService {
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    ProductMapper productMapper;

    @Autowired
    StoreUserMapper storeUserMapper;

    @Autowired
    ConsumerMapper consumerMapper;

    /**
     * 更新店铺信息
      * @param storeReq
     * @return boolean
     */
    public boolean updateStoreInfo(StoreReq storeReq) {
        Store store = new Store();
        store.setStoreId(storeReq.getStoreId());
        store.setStoreName(storeReq.getStoreName());
        store.setStoreStatus(storeReq.getStoreStatus());
        store.setBankAccountInfo(storeReq.getBankAccountInfo());
        store.setCommissionRate(storeReq.getCommissionRate());
        store.setContactEmail(storeReq.getContactEmail());
        store.setContactPersonName(storeReq.getContactPersonName());
        store.setContactPhoneNumber(storeReq.getContactPhoneNumber());
        store.setVerificationStatus(storeReq.getVerificationStatus());
        store.setJoinTime(storeReq.getJoinTime());
        int i = storeMapper.updateByPrimaryKeySelective(store);
        if (i > 0) {
            return true;
        }else {
            return false;
        }

    }

    /**
     * 新增店铺
     * @param storeReq
     * @return boolean
     */
    public boolean addStoreInfo(StoreReq storeReq) {
        Store store = new Store();
        store.setStoreName(storeReq.getStoreName());
        store.setStoreStatus(storeReq.getStoreStatus());
        store.setBankAccountInfo(storeReq.getBankAccountInfo());
        store.setCommissionRate(storeReq.getCommissionRate());
        store.setContactEmail(storeReq.getContactEmail());
        store.setContactPersonName(storeReq.getContactPersonName());
        store.setContactPhoneNumber(storeReq.getContactPhoneNumber());
        store.setVerificationStatus(storeReq.getVerificationStatus());
        store.setJoinTime(storeReq.getJoinTime());
        int i = storeMapper.insertSelective(store);
        if(i !=0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 店家查询店员
     * @param consumerId
     * @return List<StoreUserResp>
     */
    public List<StoreUserResp> getStoreUserList(int consumerId) {
        StoreUserExample storeUserExample = new StoreUserExample();
        storeUserExample.createCriteria().andConsumerIdEqualTo(consumerId);
        int storeId =  storeUserMapper.selectByExample(storeUserExample).get(0).getStoreId();
        storeUserExample = new StoreUserExample();
        storeUserExample.createCriteria().andStoreIdEqualTo(storeId);
        List<StoreUser> storeUsers = storeUserMapper.selectByExample(storeUserExample);
        List<StoreUserResp> storeUserResps = new ArrayList<>();
        for (StoreUser storeUser : storeUsers) {
            StoreUserResp storeUserResp = new StoreUserResp();
            Consumer consumer = consumerMapper.selectByPrimaryKey(storeUser.getConsumerId());
            storeUserResp.setConsumerName(consumer.getConsumerName());
            storeUserResp.setEmail(consumer.getEmail());
            storeUserResp.setPhone(consumer.getPhone());
            storeUserResp.setRole(storeUser.getRole());
            storeUserResps.add(storeUserResp);
        }
        return storeUserResps;
    }
}
